<template>
  <div class="demo">
    <h2>{{ component.data().sourceCodeTitle }}</h2>
    <div class="demo-component">
      <component :is="component" />
    </div>
    <div class="demo-actions">
      <moon-button @click="hideCode" v-if="codeVisible">隐藏代码</moon-button>
      <moon-button @click="showCode" v-else>查看代码</moon-button>
    </div>
    <div class="demo-code" v-if="codeVisible">
      <pre class="language-html" v-html="html" />
    </div>
  </div>
</template>

<script>
import 'prismjs'
import 'prismjs/themes/prism-tomorrow.css'
import MoonButton from '../lib/MoonButton.vue'
export default {
  components: {
    MoonButton
  },
  props: {
    component: Object
  },
  data() {
    return {
      codeVisible: false
    }
  },
  computed: {
    html() {
      return window.Prism.highlight(
        this.component.data().sourceCode,
        window.Prism.languages.html,
        'html'
      )
      // return this.component.data().sourceCode
    }
  },
  methods: {
    showCode() {
      this.codeVisible = true
    },
    hideCode() {
      this.codeVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
$border-color: #d9d9d9;

.demo {
  border: 1px solid $border-color;
  margin: 16px 0 32px;

  > h2 {
    font-size: 20px;
    padding: 8px 16px;
    border-bottom: 1px solid $border-color;
  }

  &-component {
    padding: 16px;
  }

  &-actions {
    padding: 8px 16px;
    border-top: 1px dashed $border-color;
  }

  &-code {
    padding: 8px 16px;
    border-top: 1px dashed $border-color;

    > pre {
      line-height: 1.1;
      font-family: Consolas, 'Courier New', Courier, monospace;
      margin: 0;
    }
  }
}
</style>
